package com.example.mattrigle;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.os.Environment;
import android.util.Log;

public class SaveData {

	
	//=============เมื่อกดปุ่มเลือกมุม
	public static String PlayerName="";
	public static String DateTime_StartTask="";// [yyyy-MM-dd HH:mm:ss]
	public static String Angle="";//มุมโจทย์ [30 45 60]
	public static String TaskNO=""; // ตอบครั้งที่ [1-4] 
	
	
	//===========เมื่อผู้ใช้ interact  กับส่วนที่ 1 ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> ID_SinCos=new ArrayList<String>();  //  [1,2]
	public static ArrayList<String> Time_ClickSinCos=new ArrayList<String>();  //  [HH:mm:ss]
		
		
	//===========เมื่อผู้ใช้ interact  กับส่วนที่ 2 ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> DragXY_Graph=new ArrayList<String>();  //  [x,y]
	public static ArrayList<String> StartTime_Touch_Graph=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> StartPosition_XY_Graph=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> StartTime_Drag_Graph=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> EndPosition_XY_Graph=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> EndTime_Drag_Graph=new ArrayList<String>();  //  [HH:mm:ss]


	//===========เมื่อผู้ใช้ interact  กับส่วนที่ ❸ ให้เก็บข้อมูลดังต่อไปนี้

	public static ArrayList<String> DragXY_SeekBar=new ArrayList<String>();  //  [x,y]
	public static ArrayList<String> StartTime_Touch_SeekBar=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> StartPosition_XY_SeekBar=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> StartTime_Drag_SeekBar=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> EndPosition_XY_SeekBar=new ArrayList<String>();  //  [(x,y)]
	public static ArrayList<String> EndTime_Drag_SeekBar=new ArrayList<String>();  //  [HH:mm:ss]


	//===========เมื่อผู้ใช้ interact  กับส่วนที่ ❹ ให้เก็บข้อมูลดังต่อไปนี้ี้

	public static ArrayList<String> Click_SinCos=new ArrayList<String>();  //  [BtnSin,BtnCos]
	public static ArrayList<String> StartTime_Click_BtnSinCos=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> Value_XY_BtnSinCos=new ArrayList<String>();  //  [(x,y)]
	

	


    //============เมื่อผู้ใช้ interact  กับส่วนที่ ❺ ให้เก็บข้อมูลดังต่อไปนี้

	public static ArrayList<String> Time_Sureness=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> Value_Sureness=new ArrayList<String>();  //  [1-4]

	
   //============เมื่อผู้ใช้ interact  กับส่วนที่ ❻ ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> Time_ScreenShot=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> NO_ScreenShot=new ArrayList<String>();  //  [1-3]

	
	
	//===========เมื่อผู้ใช้ interact  กับส่วนที่ ❼ ให้เก็บข้อมูลดังต่อไปนี้
	public static ArrayList<String> Time_OkBtn=new ArrayList<String>();  //  [HH:mm:ss]
	public static ArrayList<String> Angle_Player=new ArrayList<String>();  //  [0-90]	
	public static ArrayList<String> Angle_Discrepancy=new ArrayList<String>();  //  [30-60]

	
	// ============================ Set Format ===================================== //
	public static String setFormat(){
		String data = "Player Name = " + PlayerName +"\nDate = "+DateTime_StartTask+"\nAngle = "+Angle+"\nTaskNO = "+TaskNO+"\n";
		data = data + "Click Button Sin-Cos : \n\t";
		// +-- btn sincos --+ //
		int size = ID_SinCos.size();
		for(int i = 0; i < size; i++){
			data = data + "Click Button = [" + ID_SinCos.get(i) + " , " + Time_ClickSinCos.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		// +-- Graph --+ //
		data = data + "Graph : \n\tDragXY = (\n\t\t";
		int size2 = DragXY_Graph.size();
		for(int i = 0; i < size2; i++){
			data = data + "[" + DragXY_Graph.get(i) + "],";
		}
		data = data + "\n\tStartTime Touch = (\n\t\t";
		for(int i = 0; i < StartTime_Touch_Graph.size(); i++){
			data = data + "[" + StartTime_Touch_Graph.get(i) + "],";
		}
		data = data + ")\n\tStartPosition XY = (\n\t\t";
		for(int i = 0; i < StartPosition_XY_Graph.size(); i++){
			data = data + "[" + StartPosition_XY_Graph.get(i) + "],";
		}
		data = data + ")\n\tEndPosition XY = (\n\t\t";
		for(int i = 0; i < EndPosition_XY_Graph.size(); i++){
			data = data + "[" + EndPosition_XY_Graph.get(i) + "],";
		}
		data = data + ")\n\tStartTime Drag = (\n\t\t";
		for(int i =0;i<StartTime_Drag_Graph.size();i++){
			data = data + "[" + StartTime_Drag_Graph.get(i) + "],";
		}
		data = data + ")\n\tEndTime Drag = (\n\t\t";
		for(int i = 0; i<EndTime_Drag_Graph.size(); i++){
			data = data + "[" + EndTime_Drag_Graph.get(i) + "],";
		}
		data = data + ")\n============================================================================\n";
	
		// +-- SeekBar --+ //
		data = data + "SeekBar : \n\tDragXY = (\n\t\t";
		int size3 = DragXY_SeekBar.size();
		for(int i = 0; i < size3; i++){
			data = data + "[" + DragXY_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tStartTime Touch = (\n\t\t";
		for(int i = 0; i < StartTime_Touch_SeekBar.size(); i++){
			data = data + "[" + StartTime_Touch_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tStartPosition XY = (\n\t\t";
		for(int i = 0; i < StartPosition_XY_SeekBar.size(); i++){
			data = data + "[" + StartPosition_XY_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tEndPosition XY = (\n\t\t";
		for(int i = 0; i < EndPosition_XY_SeekBar.size(); i++){
			data = data + "[" + EndPosition_XY_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tStartTime Drag = (\n\t\t";
		for(int i =0;i<StartTime_Drag_SeekBar.size();i++){
			data = data + "[" + StartTime_Drag_SeekBar.get(i) + "],";
		}
		data = data + ")\n\tEndTime Drag = (\n\t\t";
		for(int i = 0; i<EndTime_Drag_SeekBar.size(); i++){
			data = data + "[" + EndTime_Drag_SeekBar.get(i) + "],";
		}
		data = data + ")\n============================================================================\n";
	
		// +-- SinCos --+ //
		data = data + "Click Sin Cos : \n\tClick SinCos = [\n\t\t";
		int size4 = Click_SinCos.size();
		for(int i = 0; i < size4; i++){
			data = data + "[" + Click_SinCos.get(i) + "],";
		}
		data = data + "\n\tTime Click Button SinCos = [\n\t\t";
		for(int i = 0; i < size4; i++){
			data = data + "[" + StartTime_Click_BtnSinCos.get(i) + "],";
		}
		data = data + "\n\tValue XY = [\n\t\t";
		for(int i = 0; i < size4; i++){
			data = data + "[" + Value_XY_BtnSinCos.get(i) + "],";
		}
		data = data + "\n============================================================================\n";
				
		// +-- Sureness --+ //
		data = data + "Sureness : \n\t";
		int size5 = Time_Sureness.size();
		for(int i = 0; i < size5; i++){
			data = data + "Click Button = [" + Value_Sureness.get(i) + " , " + Time_Sureness.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		// +-- ScreenShot --+ //
		data = data + "ScreenShot : \n\t";
		int size6 = Time_ScreenShot.size();
		for(int i = 0; i < size6; i++){
			data = data + "Click Button = [" + NO_ScreenShot.get(i) + " , " + Time_ScreenShot.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		// +-- Player --+ //
		data = data + "Angle Player : \n\t";
		int size7 = Time_OkBtn.size();
		for(int i = 0; i < size7; i++){
			data = data + "Click Button = [" + Angle_Player.get(i) + " , " + Angle_Discrepancy.get(i) + " , " + Time_OkBtn.get(i) + "]\n\t";
		}
		data = data + "\n============================================================================\n";
		
		return data;
	}
	
	
	// ==================================================================================== //
	public static void SaveDataTofile(){
		String filename = "TaskNO_" + TaskNO +".txt";
		String path = "/Mattrigle/" + PlayerName + "/" + Angle + "/" + DateTime_StartTask;
		
		String root = Environment.getExternalStorageDirectory().toString();
		
		File myFile = new File(root + path);
		
		String data = setFormat();
		
		try {    		
    		myFile.mkdirs();
    		
    		File newFile = new File(myFile, filename);
    		newFile.createNewFile();
    		FileOutputStream fos = new FileOutputStream(newFile);
    		OutputStreamWriter osw = new OutputStreamWriter(fos);
    		osw.write(data);
    		osw.close();
		} catch (IOException e) {
			Log.e("Emoji", "File write failed: "+e.toString());
		}
		
	}
	// ==================================================================================== //
	
	public static void clearArray(){
		ID_SinCos.clear();
		Time_ClickSinCos.clear();
		
		DragXY_Graph.clear();
		StartTime_Touch_Graph.clear();
		StartPosition_XY_Graph.clear();
		StartTime_Drag_Graph.clear();
		EndPosition_XY_Graph.clear();
		EndTime_Drag_Graph.clear();
		
		DragXY_SeekBar.clear();
		StartTime_Touch_SeekBar.clear();
		StartPosition_XY_SeekBar.clear();
		StartTime_Drag_SeekBar.clear();
		EndPosition_XY_SeekBar.clear();
		EndTime_Drag_SeekBar.clear();
		
		Click_SinCos.clear();
		StartTime_Click_BtnSinCos.clear();
		Value_XY_BtnSinCos.clear();
		
		Time_Sureness.clear();
		Value_Sureness.clear();
		
		Time_ScreenShot.clear();
		NO_ScreenShot.clear();
		
		Time_OkBtn.clear();
		Angle_Player.clear();	
		Angle_Discrepancy.clear();
		
	}
	
	public static String getTime(){
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		String currentDateandTime = sdf.format(new Date());
		return currentDateandTime;
	}
	
	public static String formatData(long data){
		Date date = new Date(data);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String currentDateandTime = sdf.format(date);
		return currentDateandTime;
	}
}





